package com.guang;

import com.guang.mapper.TemplateElementMapMapper;
import com.guang.pojo.TemplateElementMap;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;

/**
 * @author liguang
 * @date 2022/7/1 12:34
 */
public class TEST {
    @Test
    public void testQuickStart() throws IOException {
        //1. 读取全局配置文件SqlMapConfig.xml
        InputStream is = Resources.getResourceAsStream("SqlMapConfig.xml");
        //2. 创建SqlSessionFactoryBuilder构造者对象
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        //3. 使用构造者builder，根据配置文件的信息is，构造一个SqlSessionFactory工厂对象
        SqlSessionFactory factory = builder.build(is);
        //4. 使用工厂对象factory，生产一个SqlSession对象
        SqlSession session = factory.openSession();
        //5. 使用SqlSession对象，获取映射器UserDao接口的代理对象
        TemplateElementMapMapper templateElementMapMapper = session.getMapper(TemplateElementMapMapper.class);
        TemplateElementMap templateElementMap1 = new TemplateElementMap();
        templateElementMap1.setElementId(14249);
        templateElementMap1.setTemplateId("372494C0");

        TemplateElementMap templateElementMap2 = new TemplateElementMap();
        templateElementMap2.setElementId(14249);
        templateElementMap2.setTemplateId("372494C0");
        TemplateElementMap templateElementMap3 = new TemplateElementMap();
        templateElementMap3.setElementId(14249);
        templateElementMap3.setTemplateId("372494C0");
        List<TemplateElementMap> templateElementMaps = Arrays.asList(templateElementMap1, templateElementMap2, templateElementMap3);
        for (TemplateElementMap templateElementMap : templateElementMaps) {
            templateElementMap.setDelFlag(0);
            templateElementMap.setVersion(0L);
            templateElementMapMapper.insert(templateElementMap);
        }
        session.commit(true);
        //7. 释放资源
        session.close();
        is.close();
    }
}
